#include "widget.h"
#include "./ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    QDateTime currentDateTime = QDateTime::currentDateTime();
    ui->timeEdit->setTime(currentDateTime.time());
    ui->editTime->setText(currentDateTime.toString("hh:mm:ss"));

    ui->dateEdit->setDate(currentDateTime.date());
    ui->editDate->setText(currentDateTime.toString("yyyy-MM-dd"));

    ui->dateTimeEdit->setDateTime(currentDateTime);
    ui->editDateTime->setText(currentDateTime.toString("yyyy-MM-dd hh:mm:ss"));
}


void Widget::on_btnQDebugTime_clicked()
{
    QTime tm1(13, 24, 5);
    QString str = tm1.toString("HH:mm:ss");
    qDebug("Original time = %s", str.toLocal8Bit().data());

    QTime tm2 = tm1.addSecs(150);
    str = tm2.toString("HH:mm:ss");
    qDebug("150s later, time = %s", str.toLocal8Bit().data());

    tm2 = QTime::currentTime();
    str = tm2.toString("HH:mm:ss");
    qDebug("Current time = %s", str.toLocal8Bit().data());
    qDebug("Hour = %d, miniute = %d, second = %d", tm2.hour(), tm2.minute(), tm2.second());

}


void Widget::on_btnQDebugDate_clicked()
{
    QDate dt1(2021, 7, 6);
    QString str = dt1.toString("yyyy-MM-dd");
    qDebug("dt1 = %s", str.toLocal8Bit().data());

    QDate dt2;
    dt2.setDate(2021, 8, 25);
    str = dt2.toString("yyyy-MM-dd");
    qDebug("dt2 = %s", str.toLocal8Bit().data());
    // dt2与dt1相差天数
    qDebug("Days between dt2 and dt1 = %d", dt2.daysTo(dt1));

    dt2 = QDate::currentDate();
    str = dt2.toString("yyyy-MM-dd");
    qDebug("Current date = %s", str.toLocal8Bit().data());
    qDebug("Year = %d, Month = %d, Day = %d, Day of week = %d",
           dt2.year(), dt2.month(), dt2.day(),
           dt2.dayOfWeek()); // 1表示星期一, 7表示星期日
}


void Widget::on_btnQDebugDateTime_clicked()
{
    QDateTime dt1 = QDateTime::currentDateTime();
    QString str = dt1.toString("yyyy-MM-dd hh:mm:ss");
    qDebug("dt1 = %s", str.toLocal8Bit().data());

    QDate dt = dt1.date(); // 日期部分
    str = dt.toString("yyyy-MM-dd");
    qDebug("dt1.date() = %s", str.toLocal8Bit().data());

    QTime tm = dt1.time(); // 时间部分
    str = tm.toString("hh:mm:ss");
    qDebug("tm.time() = %s", str.toLocal8Bit().data());

    qint64 ms = dt1.toSecsSinceEpoch(); // 转换为秒数
    qDebug("dt1.toSecsSinceEpoch() = %lld", ms);
}


void Widget::on_btnSetTime_clicked()
{
    QString str = ui->editTime->text();
    str = str.trimmed();
    if (!str.isEmpty()) {
        QTime tm = QTime::fromString(str, "hh:mm:ss");
        ui->timeEdit->setTime(tm);
    }
}


void Widget::on_btnSetDate_clicked()
{
    QString str = ui->editDate->text();
    str = str.trimmed();
    if (!str.isEmpty()) {
        QDate dt = QDate::fromString(str, "yyyy-MM-dd");
        ui->dateEdit->setDate(dt);
    }
}


void Widget::on_btnSetDateTime_clicked()
{
    QString str = ui->editDateTime->text();
    str = str.trimmed();
    if (!str.isEmpty()) {
        QDateTime dt = QDateTime::fromString(str, "yyyy-MM-dd hh:mm:ss");
        ui->dateTimeEdit->setDateTime(dt);
    }
}


void Widget::on_calendarWidget_selectionChanged()
{
    QString str = ui->calendarWidget->selectedDate().toString("yyyy年M月d日");
    ui->editCalendar->setText(str);
}

